package com.puzzlebazar.client.main.control;

import com.puzzlebazar.client.main.model.PuzzleListInfo;
import com.puzzlebazar.client.main.view.MainView;
import com.puzzlebazar.client.main.view.SearchView;
import com.puzzlebazar.client.main.view.SearchView.SearchCallback;
import com.puzzlebazar.client.user.UserModel;

public class SearchState extends MainState {

	public static final String STATE_TOKEN = "Search";
	
	private SearchView searchView = null;

	private final MySearchCallback searchCallback = new MySearchCallback();

	
	public SearchState( UserModel model, MainView view, MainController controller ) {
		super( model, view, controller );
	}

	@Override
	public String getStateToken() {
		return STATE_TOKEN;
	}
	
	@Override
	public void enter() {
		view.setMainPanelTitle( "Search" );
		
		if( searchView == null )
			searchView = new SearchView();
		searchView.resetState();
		
		view.setMainWidget( searchView );

		enableCallbacks();
	}

	@Override
	public void exit() {
		view.setMainPanelTitle( null );
		
		disableCallbacks();
	}


	public class MySearchCallback implements SearchCallback {

		@Override
		public void cancel() {
			controller.backToPreviousState();
		}

		@Override
		public void search( PuzzleListInfo searchListInfo ) {
			controller.switchToPuzzleList( searchListInfo );
		}

	}
	
	/**
	 * Turns all callbacks on
	 */
	private void enableCallbacks() {
		searchView.addCallback( searchCallback );
	}

	/**
	 * Turns all defaults callbacks off
	 */
	private void disableCallbacks() {
		searchView.removeCallback( searchCallback );
	}

	@Override
	public void release() {
		super.release();
		searchView = null;
	}
}
